//
//  XYCollectionView.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/4/13.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYCollectionView.h"
#import "XYColorCollectionViewCell.h"
@interface XYCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;


@end
static NSString *const kXYColorCollectionViewCell = @"XYColorCollectionViewCell";
@implementation XYCollectionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self collectionView];
    }
    return self;
}
- (void)reloadData{
    [self.collectionView reloadData];
}
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *>*)indexPaths{
    [self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.currentLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator =NO;
        _collectionView.showsVerticalScrollIndicator =NO;
        [_collectionView registerNib:[UINib nibWithNibName:kXYColorCollectionViewCell bundle:nil] forCellWithReuseIdentifier:kXYColorCollectionViewCell];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return _collectionView;
}
-(UICollectionViewFlowLayout *)currentLayout{
    if (!_currentLayout) {
        _currentLayout = [[UICollectionViewFlowLayout alloc] init];
        _currentLayout.minimumLineSpacing = 10;
        _currentLayout.minimumInteritemSpacing = 10;
        _currentLayout.itemSize = CGSizeMake(XYScreenWidth, 50);
        _currentLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _currentLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _currentLayout;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XYColorCollectionViewCell *cell = (XYColorCollectionViewCell *)[self.delegate xy_collectionView:self cellForItemAtIndexPath:indexPath];
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:kXYColorCollectionViewCell forIndexPath:indexPath];
    }
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
- (UICollectionViewCell *)xy_dequeueReusableCellWithReuseIdentifier:(NSString *)reuseIdentifier forIndexPath:(NSIndexPath *)indexPath{
    return [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    XYColorCollectionViewCell *cell = (XYColorCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.selected = !cell.selected;
    if ([self.delegate respondsToSelector:@selector(xy_collectionView:didSelectItemAtIndexPath:)]) {
        [self.delegate xy_collectionView:self didSelectItemAtIndexPath:indexPath];
    }
}
- (void)setItemSize:(CGSize)itemSize{
    _itemSize = itemSize;
    _currentLayout.itemSize = itemSize;
}
- (void)setSectionInset:(UIEdgeInsets)sectionInset{
    _sectionInset = sectionInset;
    _currentLayout.sectionInset = sectionInset;
}
@end












